/*
 * Copyright 2008 Robert Hanson <iamroberthanson AT gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.penlets.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * A basic implementation of the java.util.Properties
 * class for use in penlets.
 * @author robert.hanson
 */
public class Properties
{

    private Hashtable propTable = new Hashtable();


    /**
     * Creates a new Properties file from a String.
     * @param data a String to parse properties from
     * @return a Properties file
     */
    public static Properties loadPropertiesFromString (String data)
    {
        Properties result = new Properties();

        String[] lines = StringUtils.split(data, '\n', 0);
        for (int i = 0; i < lines.length; i++) {
            String[] kv = StringUtils.split(StringUtils.chomp(lines[i]), '=', 2);
            if (kv.length == 1) {
                result.setProperty(kv[0], "");
            }
            if (kv.length == 2) {
                result.setProperty(kv[0], kv[1]);
            }
        }
        
        return result;
    }

    /**
     * Creates a Properties object from a file located in the penlet JAR.
     * @param filePath path to the file
     * @return a Properties instance
     * @throws IOException
     */
    public static Properties loadProperties (String filePath) throws IOException
    {
        Properties result = new Properties();

        InputStream stream = result.getClass().getResourceAsStream(filePath);
        InputStreamReader reader = new InputStreamReader(stream);

        StringBuffer sBuf = new StringBuffer();
        char[] buff = new char[1024];

        int pos = reader.read(buff, 0, 1024);
        while (pos != -1) {
            sBuf.append(buff, 0, pos);
            pos = reader.read(buff, 0, 1024);
        }

        String[] lines = StringUtils.split(sBuf.toString(), '\n', 0);
        for (int i = 0; i < lines.length; i++) {
            String[] kv = StringUtils.split(StringUtils.chomp(lines[i]), '=', 2);
            if (kv.length == 1) {
                result.setProperty(kv[0], "");
            }
            if (kv.length == 2) {
                result.setProperty(kv[0], kv[1]);
            }
        }
        
        return result;
    }


    /**
     * Sets a property. Setting properties will not be persisted
     * to the underlying file, they will simply override any existing
     * value in memory.
     * @param key property name
     * @param val property value
     */
    public void setProperty (String key, String val)
    {
        this.propTable.put(key, val);
    }

    /**
     * Gets a property.
     * @param key property name
     * @return property value
     */
    public String getProperty (String key)
    {
        return (String) this.propTable.get(key);
    }

    /**
     * Returns a count of properties.
     * @return property count
     */
    public int getPropertyCount ()
    {
        return this.propTable.size();
    }

    /**
     * Gets an Enumeration of properties.
     * @return an Enumeration of properties
     */
    public Enumeration getEnumeratedNames ()
    {
        return this.propTable.keys();
    }

    /**
     * Gets an array of property names.
     * @return an array of property names
     */
    public String[] getPropertyNames ()
    {
        String[] result = new String[this.propTable.size()];
        int c = 0;
        for (Enumeration e = this.propTable.keys(); e.hasMoreElements();) {
            result[c] = (String) e.nextElement();
            c++;
        }
        return result;
    }

}
